<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        
        #one {
            background-color: rgb(121, 38, 255);
            width: 500px;
            height: 50px;
            margin: 5px auto;
            text-align: center;
            position: relative;
        }
        
        span {
            margin: 0 15px;
            font-size: 30px;
            line-height: 50px;
        }
        
        #tiao {
            background-color: rgb(58, 255, 189);
            height: 10px;
            width: 100px;
            position: absolute;
            bottom: -15px;
            transform: translateX(0px);
            transition: all 1s ease;
        }
    </style>
</head>

<body>
    <div id="one">
        <span>新闻</span>
        <span>体育</span>
        <span>娱乐</span>
        <span>历史</span>
        <span>军事</span>
        <div id="tiao"></div>
    </div>

</body>

</html>
<script>
    var span1 = document.getElementsByTagName('span');
    var tiao = document.getElementById('tiao');
    a = 0;
    for (i = 0; i <= 4; i++) {
        span1[i].onclick = function() {
            console.log(i);
            for (i = 0; i <= 4; i++) {
                if (span1[i] == this) {

                    a = i
                    console.log(this);
                }
            }
            for (j = 0; j <= 4; j++) {
                if (j == a) {
                    console.log(a);
                    tiao.style.transform = "translateX(" + 100 * a + "px)";
                }

            }
        }
    }
</script>